# 多线程获取电影天堂的最新电影信息
import datetime
import json
import re
import threading
from threading import Thread

import requests
from bs4 import BeautifulSoup


def get_url(urls):
    url = None
    lock.acquire()
    if len(urls) != 0:
        url = urls[0]
        urls.remove(url)
    lock.release()
    return url


class MyThread(Thread):
    def __init__(self, name):
        Thread.__init__(self)
        self.name = name

    def run(self):
        headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15',
        }
        movie_record = open('movie_record.txt', 'a')
        movie_record_error = open('movie_error.txt', 'a')
        while True:
            url_info = get_url(urls)
            if url_info:
                response = None
                print(self.name, f'正在处理链接：{url_info}')
                try:
                    response = requests.get(url=url_info, headers=headers, timeout=5)
                except Exception as e:
                    movie_record_error.write(f'【超时警告】：{url_info}\n')
                    continue
                response.encoding = 'gb2312'
                soup = BeautifulSoup(response.text, 'lxml')
                items = soup.select('.co_content8 .ulink')
                movie_info_array = []
                for item in items:
                    movie_url = 'https://www.dytt8.net' + item.get('href')
                    try:
                        movie_title = re.findall('.*《(.*?)》.*', item.string)[0]
                    except Exception as e:
                        movie_record_error.write(f'【适配警告】，无法获取电影名称，链接：{movie_url}\n')
                    movie_info = f'电影名称：《{movie_title}》,电影网页链接：{movie_url}'
                    movie_info_array.append(movie_info)
                lock.acquire()
                movie_info_array.append('')
                movie_record.write('\n'.join(movie_info_array))
                lock.release()
            else:
                break


if __name__ == '__main__':
    global urls
    starttime = datetime.datetime.now()
    lock = threading.Lock()
    urls = [f'https://www.dytt8.net/html/gndy/dyzz/list_23_{i}.html' for i in range(1, 252, 1)]

    thread1 = MyThread('Thead-001')
    thread2 = MyThread('Thead-002')
    thread3 = MyThread('Thead-003')
    thread4 = MyThread('Thead-004')
    thread1.start()
    thread2.start()
    thread3.start()
    thread4.start()
    thread1.join()
    thread2.join()
    thread3.join()
    thread4.join()
    endtime = datetime.datetime.now()
    print(f'提示：电影天堂信息获取完毕，共耗时{(endtime-starttime)}')